/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.libs.LibEvent;
import com.bourre.ioc.loader.ApplicationLoaderEvent;
import com.bourre.ioc.loader.IApplicationLoaderListener;
import com.bourre.ioc.utils.GlobalUtils;

import fever.data.libs.FileDecrypter;
import fever.Fever;
import fever.FeverApplication;
import fever.ioc.conf.CoreConfiguration;
import fever.ioc.loader.SecureApplicationLoader;
import fever.utils.Stringifier;

/**
 * Fever <strong>I.O.C Abstract Assembler</strong>.
 * 
 * <ul>
 *   <li>Act as basic <strong>Fever</strong> application mixing I.O.C. 
 *   features.</li>
 *   <li>Allow to load crypted application context file.</li>
 * </ul>
 * 
 * <p>Extends class to add custom features during I.O.C. assembling process.
 * 
 * <p>Custom assembler sample :
 * {@code
 *   class com.myapp.MyCore extends FeverCore
 *   {
 *		//MTASC Access   	
 *		public static function main() : Void
 *   	{
 *   		Fever.run( new MyCore(), BrowserContext.getInstance(), true );
 *   	}
 *   	
 *		//Private constructor here
 *   	private function MyCore() { super( ); }
 * 
 *   	//Loads encrypted context file
 *   	private function _getFileCrypter(  ) : FileDecrypter 
 *   	{
 *   		return new FileDecrypter( RC4.getProcess(), 'mySecretKey' );
 *   	}
 *   }
 * }
 * 
 * @author Romain Ecarnot
 */
class fever.ioc.plugin.FeverCore implements FeverApplication, 
	IApplicationLoaderListener
{
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Triggered when core is ready and optional configuration is loaded.
	 */
	public function run() : Void 
	{
		//Defines Manager properties
		setPluginRepository( Fever.application.config.pluginRepository );
		setPluginManagerChannel( Fever.application.config.pluginManagerChannel );
		setPluginManagerID( Fever.application.config.pluginManagerID );
		
		//Inits IOC global properties
		GlobalUtils.initGlobals();
		
		//Loads context file
		var al : SecureApplicationLoader = new SecureApplicationLoader( 
			_getFileCrypter() 
		);
		al.addListener( this );
		al.load();
	}
	
	/**
	 * Sets the global plugins repository path.
	 * 
	 * @param path Relative path
	 */
	public function setPluginRepository( path : String ) : Void
	{
		CoreConfiguration.pluginRepository = ( path + '/' );	
	}
	
	/**
	 * Returns the global plugins repository path.
	 */
	public function getPluginRepository(  ) : String
	{
		return CoreConfiguration.pluginRepository;
	}
	
	/**
	 * Sets the Manager plugin channel.
	 * 
	 * @param channel Plugin channel
	 */
	public function setPluginManagerChannel( channel : String ) : Void
	{
		CoreConfiguration.managerChannel = channel;	
	}
	
	/**
	 * Returns Manager plugin channel.
	 */
	public function getPluginManagerChannel(  ) : String
	{
		return CoreConfiguration.managerChannel;
	}
	
	/**
	 * Sets the Manager plugin id.<br />
	 * ( used in Bean definition ).
	 * 
	 * @param path folder relative path
	 */
	public function setPluginManagerID( id : String ) : Void
	{
		CoreConfiguration.managerID = id;	
	}
	
	/**
	 * Returns the Manager plugin ID.<br />
	 * ( used in Bean definition ).
	 */
	public function getPluginManagerID(  ) : String
	{
		return CoreConfiguration.managerID;
	}
	
	/**
	 * IApplicationLoaderListener callbacks
	 */
	public function onApplicationBuilt( e : ApplicationLoaderEvent ) : Void
	{
		
	}

	/**
	 * IApplicationLoaderListener callbacks
	 */
	public function onApplicationInit( e : ApplicationLoaderEvent ) : Void
	{
		
	}
	
	/**
	 * IApplicationLoaderListener callbacks
	 */
	public function onLoadInit( e : LibEvent ) : Void
	{
		
	}
	
	/**
	 * IApplicationLoaderListener callbacks
	 */
	public function onLoadProgress( e : LibEvent ) : Void
	{
		
	}
	
	/**
	 * IApplicationLoaderListener callbacks
	 */
	public function onTimeOut( e : LibEvent ) : Void
	{
		
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */ 
	private function FeverCore( ) { }
	
	/**
	 * Returns {@link fever.data.libs.FileDecrypter} if need it for 
	 * xml context loading. 
	 * 
	 * <p>Default returns {@code null} crypter.<br />
	 * Overrides to custom it.
	 * 
	 * <p>Example :
	 * {@code
	 *   return new FileDecrypter( RC4.getProcess(), 'encryptionKey' );
	 * }
	 */
	private function _getFileCrypter(  ) : FileDecrypter 
	{
		return null;
	}
}